package com.yls.except.service.impl;


import com.yls.except.dao.ImgDao;
import com.yls.except.dao.SystemDao;
import com.yls.except.dao.UserDao;
import com.yls.except.service.SystemService;
import com.yls.except.util.PageUtil;
import com.yls.except.util.StringIsempty;
import com.yls.except.util.ValueJude;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 消息serviceimpl
 *
 * @author xs
 * @version 1.0
 * @time 2016-3-1 下午3:55:03
 * @Description: {}
 */
@Service
@Repository(value = "systemService")
@Transactional
public class SystemServiceImpl implements SystemService {
    @Resource(name = "systemDao")
    private SystemDao systemDao;
    @Resource(name = "imgDao")
    private ImgDao imgDao;
    @Resource(name = "userDao")
    private UserDao userDao;


    @Override
    public List<Object> selectProvinceList() {
        List<Map<String, Object>> province = systemDao.selectProvinceList();
        List<Object> list = new ArrayList<>();
        if (province != null && province.size() > 0)
            list.addAll(province);
        return list;
    }

    @Override
    public List<Object> selectCityList(String pid) {
        List<Map<String, Object>> city;
        if (StringIsempty.isallempty(pid))
            city = systemDao.selectCityList();
        else
            city = systemDao.selectCityListByPid(pid);
        List<Object> list = new ArrayList<>();
        if (city != null && city.size() > 0)
            list.addAll(city);
        return list;
    }

    @Override
    public List<Object> selecDistrictList(String cid) {
        List<Map<String, Object>> district = systemDao.selectDistrictListByCid(cid);
        List<Object> list = new ArrayList<>();
        if (district != null && district.size() > 0)
            list.addAll(district);
        return list;
    }

    @Override
    public List<Object> selecDistrictListByCityName(String cityname) {
        if (StringIsempty.isallempty(cityname))
            cityname = "重庆市";
        cityname = "'%" + cityname + "%'";
        Map<String, Object> citymap = new HashMap<String, Object>();
        citymap.put("cityname", cityname);
        Map<String, Object> city = systemDao.selectCityInfoByName(citymap);   //根据市名称获取区信息
        if (city == null || city.size() <= 0)
            return null;
        String cid = city.get("cid") + "";
        if (StringIsempty.isallempty(cid))
            return null;
        List<Map<String, Object>> district = systemDao.selectDistrictListByCid(cid);
        List<Object> list = new ArrayList<>();
        if (district != null && district.size() > 0)
            list.addAll(district);
        return list;
    }

    @Override
    public int insertAddress(String uid, String name, String phone, String pid, String cid, String did, String address, String postcode, String reqip) {
        Map<String, Object> addressinfo = new HashMap<String, Object>();
        addressinfo.put("address_id", ValueJude.getUuid());
        addressinfo.put("uid", uid);
        addressinfo.put("pid", pid);
        addressinfo.put("cid", cid);
        addressinfo.put("did", did);
        addressinfo.put("address", address);
        addressinfo.put("isdefault", 0);
        addressinfo.put("phone", phone);
        addressinfo.put("name", name);
        addressinfo.put("postcode", postcode);
        addressinfo.put("addtime", new Date().getTime());
        addressinfo.put("addip", reqip);
        addressinfo.put("isdisabled", 1);
        return systemDao.insertAddress(addressinfo);
    }

    @Override
    public List<Object> selectMyAddress(String uid, String page, String searchname, String count) {
        int nowpage = 0;
        int total = 10;
        if(!StringIsempty.isallempty(count))
            total = Integer.parseInt(count);
        if (!StringIsempty.isallempty(page))
            nowpage = Integer.parseInt(page);
        Map<String, Object> map = PageUtil.getNowPage(nowpage, total);
        map.put("uid", uid);
        if (!StringIsempty.isallempty(searchname))
            map.put("msg", " and address like '%" + searchname + "%'");
        else
            map.put("msg", "");
        List<Map<String, Object>> addressinfo = systemDao.selectMyAddress(map);
        List<Object> list = new ArrayList<Object>();
        if(!StringIsempty.isNullList(addressinfo)) {
            for (int i = 0; i < addressinfo.size(); i ++) {
                Map<String, Object> address = addressinfo.get(i);
                if(StringIsempty.isNullMap(address))
                    continue;
                getAddress(address, systemDao);
            }
            list.addAll(addressinfo);
        }
        return list;
    }

    @Override
    public int updateAddressinfo(String uid, String addressid, String name, String phone, String pid, String cid, String did, String address, String postcode, String isdefault) {
        Map<String, Object> addressinfo = systemDao.selectAddressInfoById(addressid);
        if(StringIsempty.isNullMap(addressinfo))
            return -13;
        if (!StringIsempty.isallempty(name))
            addressinfo.put("name", name);
        if (!StringIsempty.isallempty(phone))
            addressinfo.put("phone", phone);
        if (!StringIsempty.isallempty(pid))
            addressinfo.put("pid", pid);
        if (!StringIsempty.isallempty(cid))
            addressinfo.put("cid", cid);
        if (!StringIsempty.isallempty(did))
            addressinfo.put("did", did);
        if (!StringIsempty.isallempty(address))
            addressinfo.put("address", address);
        if (!StringIsempty.isallempty(postcode))
            addressinfo.put("postcode", postcode);
        if("1".equals(isdefault)) {
            addressinfo.put("isdefault", 1);
            systemDao.updateAddressIsDefault(uid);
        }
        systemDao.updateAddressInfo(addressinfo);
        return 1;
    }

    @Override
    public int deleteAddressInfo(String uid, String addressid) {
        Map<String, Object> addressinfo = systemDao.selectAddressInfoById(addressid);
        if(StringIsempty.isNullMap(addressinfo))
            return -13;
        if(!uid.equals(addressinfo.get("uid") + ""))
            return -43;
        addressinfo.put("isdisabled", 0);
        systemDao.updateAddressInfo(addressinfo);
        return 1;
    }

    @Override
    public Map<String, Object> selectAddressInfoById(String addressid) {
        Map<String, Object> addressinfo = systemDao.selectAddressInfoById(addressid);
        if(StringIsempty.isNullMap(addressinfo))
            return null;
        getAddress(addressinfo, systemDao);
        addressinfo.put("tag", 1);
        return addressinfo;
    }

    @Override
    public Map<String, Object> selectSystemSetInfoById(String systemsetid, String uid) {
        Map<String, Object> systemsetinfo = systemDao.selectSystemSetInfoById(systemsetid);
        if(StringIsempty.isNullMap(systemsetinfo))
            return null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("setvalue", systemsetinfo.get("setvalue") + "");
        long sparemoney = 0;
        if(StringIsempty.isallempty(uid))
            map.put("sparemoney", 0);
        else {
            Map<String, Object> useraccout = userDao.selectUserAccountByUid(uid);
            if(StringIsempty.isNullMap(useraccout))
                map.put("sparemoney", 0);
            else {
                map.put("sparemoney", ValueJude.getDecima(useraccout.get("sparemoney") + ""));
                sparemoney = Long.parseLong(useraccout.get("sparemoney") + "");
            }
        }
        long setvalue = Long.parseLong(systemsetinfo.get("setvalue") + "") * 100;
        if(sparemoney >= setvalue)
            map.put("isenough", true);
        else
            map.put("isenough", false);
        map.put("tag", 1);
        return map;
    }

    @Override
    public Map<String, Object> selectUserinfoById(String uid) {
        return userDao.selectUserInfoByUid(uid);
    }

    public static void getAddress(Map<String, Object> addressinfo, SystemDao systemDao) {
        String pid = addressinfo.get("pid") + "";
        String cid = addressinfo.get("cid") + "";
        String did = addressinfo.get("did") + "";
        Map<String, Object> province = systemDao.selectProvinceById(pid);
        Map<String, Object> city = systemDao.selectCityById(cid);
        Map<String, Object> district = systemDao.selectDistrictById(did);
        String address = addressinfo.get("address") + "";
        if(!StringIsempty.isNullMap(district)) {
//            address = district.get("name") + address;
            addressinfo.put("districtname", district.get("name"));
        } else
            addressinfo.put("districtname", "");
        if(!StringIsempty.isNullMap(city)) {
//            address = city.get("name") + address;
            addressinfo.put("cityname", city.get("name"));
        } else
            addressinfo.put("cityname", "");
        if(!StringIsempty.isNullMap(province)) {
//            address = province.get("name") + address;
            addressinfo.put("provincename", city.get("name"));
        } else
            addressinfo.put("provincename", "");
        addressinfo.put("address", address);
    }
}
